<template>
  <div class="template">
    <hunk-head icon="home" name="合同详情"></hunk-head>
    <div @click="readContract" class="contractPdf">
              <image v-if="baseUrl" :src="baseUrl+'pdf.png'" alt="" class="pdfStyle" />
          <p>{{info.code}}</p>

          <div style="margin-top:20rpx;font-size: 24rpx;">
            点击上方文件，查看合同
          </div>
    </div>
      <view style="display: flex;align-items: center;width: 80%;margin: 40rpx auto;">
          <radio-group>
            <radio :value="isAgree" :checked="isAgree" @click.stop="radioChange" />
          </radio-group>
          <view style="font-size: 24rpx;text-align: left;">
            我已仔细阅读并同意本页面显示的合同
          </view>
        </view>
    <div :class="{'btn-round':true, inactive:!isAgree}" @click="bindSign">签名</div>


    <uni-popup ref="approve" type="center">
      <div class="tips2">
        <div class="tips-top">提示</div>
        <div class="tips-text">尊敬的用户，您还未进行用户授权，是否进行用户授权？</div>
        <div class="tips-btn">
          <div class="btn-closs" @click="closeP">取消</div>
          <div  @click="checkedP">确认</div>
        </div>
      </div>
    </uni-popup>

  </div>
</template>

<script>
import Head from '@componentsMine/head'
import { tEContractInfo } from "@/apis/contract"
import store from '@store/index'
import {GetUserInfo_openid,getuseropenid} from '@/apis/user'
import { setToken } from '@utils/auth'
import {debounce} from 'lodash'

export default {
  components: {
    'hunk-head': Head
  },
  data() {
    return {
      pdfPath:'',
      isAgree:false,
      eid:'',
      isRead:false,
      info:{}
    }
  },
  computed:{
    userInfo() {
      return this.$store.state.baseStore.userInfo
    },
       
    baseUrl() {
      return process.env.VUE_APP_BASE_IMG_URL
    },
 
  },
  onLoad(el){
      this.eid = el.eid
      this.isShouquan().then(e=>{
        this.getInfo()
      })
  },
  created(){
  },
  methods: {
    checkedP(){
      this.$refs.approve.close()
      uni.navigateTo({ url: '/pages/link/approve' })
    },
    closeP(){
      this.$refs.approve.close()
      uni.reLaunch({ url: '/pages/login/checkUser' })
    },
    getInfo(){
      tEContractInfo({
        eid:this.eid
      }).then(e=>{
        if(e.code==200){
          this.info = e.data
        }
      })
    },
    isShouquan(){
      return new Promise((reslove,reject)=>{
          let that = this
          wx.login({
            success:(res)=> {
              console.log('login',res)
              var code = res.code
              getuseropenid({code}).then(e=>{
                console.log('getuseropenid',typeof(e.data.openid),e.data.openid)
                // if(e.data.openid != 'null'){
                  wx.setStorageSync('openid', e.data.openid)
                  wx.setStorageSync('session_key', e.data.session_key)
                  GetUserInfo_openid({openid:e.data.openid}).then(e=>{
                    console.log('执行GetUserInfo_openid',e)
                    that.isCode = true
                    if(e.code == 200){
                      store.commit('baseStore/SET_USERINFO', e.data)
                      setToken(e.data.token)
                    }else if(e.code == 300){
                      that.$refs.approve.open()
                    }
                    reslove()
                  }).catch(e=>{
                    
                  })
                // }else{
                //   that.isCode = false
                //   that.$refs.wxpopup.open()
                // }
              })
            }
          })
      })
    },
    readContract:debounce(function(){
        uni.showLoading({
          title: '加载中...'
      })
      uni.downloadFile({
        url: this.info.fileurl,
        success:(res)=> {
          var filePath = res.tempFilePath;
          this.pdfPath = filePath
          uni.openDocument({
            filePath: filePath,
            showMenu: true,
            success:(res) =>{
              uni.hideLoading()
              this.isRead = true
              console.log('打开文档成功');
            }
          });
        }
      });
    },1000),
      radioChange(){
        this.isAgree = !this.isAgree 
      },
      bindSign(){
        if(!this.isAgree){
          uni.showToast({
            title: '请先同意合同',
            icon: 'none'
          })
          return false
        }
        if(!this.isRead){
          uni.showToast({
            title: '请先阅读合同',
            icon: 'none'
          })
          return false
        }
        uni.navigateTo({
          url: '/pages/sign/index?eid='+this.eid
        })
      }
  }
}
</script>

<style lang="scss" scoped>
.template {
  @include num();
  background: #F9F9F9;
  display: flex;
  flex-direction: column;
  .pdf{
      height:450px;
  }
  .btn-round.inactive{
    background: #ccc;
    box-shadow: none;
  }
  .btn-round {
    width: 345px;
    height: 45px;
    font-size: 16px;
    text-align: center;
    line-height: 45px;
    margin: 0 auto;
    color: #fff;
    background: linear-gradient(315deg, #FFAA58 0%, #FFBD6C 100%);
    box-shadow: 0px 2px 4px 0px rgba(252, 174, 55, 0.21), 0px 3px 5px 0px rgba(255, 216, 111, 0.14);
    border-radius: 100px;
  }
}
.contractPdf{
  width: 375px;
  height: 183px;
  text-align: center;
  background: #FFFFFF;
  p{
    color:#0191FF;
  }
}
.pdfStyle{
  width:72px;
  margin-top:40px;
  height:72px;
}
.tips2{
    width: 335px;
    height: 174px;
    display: flex;
    flex-direction: column;
    align-items: center;
    border-radius: 10px;
    background-color: #fff;
    position: relative;
    .tips-top{
      height: 22px;
      font-size: 16px;
      font-family: PingFangSC-Medium, PingFang SC;
      font-weight: 500;
      color: #4A4A4A;
      line-height: 22px;
      margin: 20px 0 30px 0;
    }
    .tips-text{
      height: 22px;
      font-size: 16px;
      font-family: PingFangSC-Regular, PingFang SC;
      font-weight: 400;
      color: #6D7278;
      padding: 0 20px;
      line-height: 22px;
    }
    .tips-btn{
      width: 100%;
      border-top: 2px solid #EFEFEF;
      display: flex;
      position: absolute;
      bottom: 0;
      div{
        height: 40px;
        width: 50%;
        font-size: 13px;
        font-family: PingFangSC-Medium, PingFang SC;
        font-weight: 500;
        color: #4A4A4A;
        line-height: 40px;
        text-align: center;
      }
      .btn-closs{
        border-right: 2px solid #EFEFEF;
      }
    }
  }
</style>
